package com.ruoyi.mp.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.db.domain.system.PcComboPro;
import com.ruoyi.db.domain.system.PcGoldConfig;
import com.ruoyi.db.domain.system.PcProCombo;
import com.ruoyi.db.domain.system.PcProduct;
import com.ruoyi.db.mapper.system.PcComboProMapper;
import com.ruoyi.db.mapper.system.PcGoldConfigMapper;
import com.ruoyi.db.mapper.system.PcProComboMapper;
import com.ruoyi.db.mapper.system.PcProductMapper;
import com.ruoyi.mp.service.IPcProComboService;
import org.checkerframework.checker.units.qual.N;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 【产品套餐】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-23
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class PcProComboServiceImpl implements IPcProComboService
{
    @Autowired
    private PcProComboMapper pcProComboMapper;
    @Autowired
    private PcProductMapper pcProductMapper;
    @Autowired
    private PcGoldConfigMapper pcGoldConfigMapper;
    @Autowired
    private PcComboProMapper pcComboProMapper;

    /**
     * 查询【产品套餐】
     * 
     * @param proComboId 【产品套餐】主键
     * @return 【产品套餐】
     */
    @Override
    public PcProCombo selectPcProComboByProComboId(Long proComboId)
    {
        PcProCombo pcProCombo = pcProComboMapper.selectPcProComboByProComboId(proComboId);
        if(pcProCombo != null){
            BigDecimal rmbPrice = pcProCombo.getRmbPrice()==null?new BigDecimal(0):pcProCombo.getRmbPrice();
            if(pcProCombo.getReturnRate()!=null){
                pcProCombo.setReturnGold((pcProCombo.getReturnRate()).setScale( 2, BigDecimal.ROUND_HALF_UP ));
            }else {
                pcProCombo.setReturnGold(new BigDecimal(0));
            }
            PcComboPro child = new PcComboPro();
            child.setProComboId(pcProCombo.getProComboId());
            List<PcComboPro> cpList = pcComboProMapper.selectPcComboProList(child);
            List<PcProduct> products = new ArrayList<>();
            BigDecimal proAllAmount = new BigDecimal("0");
            for (PcComboPro cp: cpList) {
                PcProduct pcProduct = pcProductMapper.selectPcProductByProId(cp.getProId());
                pcProduct.setComboAmount(cp.getComboAmount());
                products.add(pcProduct);
                proAllAmount=proAllAmount.add(pcProduct.getRmbPrice().multiply(new BigDecimal(cp.getComboAmount())));
            }
            pcProCombo.setProducts(products);
            pcProCombo.setTotalProAmount(proAllAmount);
            return pcProCombo;
        }
        return null;
    }

    /**
     * 查询【产品套餐】列表
     * 
     * @param pcProCombo 【产品套餐】
     * @return 【产品套餐】
     */
    @Override
    public List<PcProCombo> selectPcProComboList(PcProCombo pcProCombo)
    {
        List<PcProCombo> pros =  pcProComboMapper.selectPcProComboListOrderBySort(pcProCombo);
        for (PcProCombo pro: pros) {
            if(pro.getReturnRate()!=null){
                BigDecimal rmbPrice = pro.getRmbPrice()==null?new BigDecimal(0):pro.getRmbPrice();
                pro.setReturnGold((pro.getReturnRate()).setScale( 2, BigDecimal.ROUND_HALF_UP ));
            }else{
                pro.setReturnGold(new BigDecimal(0));
            }
            PcComboPro pcComboPro = new PcComboPro();
            pcComboPro.setProComboId(pro.getProComboId());
            List<PcComboPro> cpList = pcComboProMapper.selectPcComboProList(pcComboPro);
            BigDecimal proAllAmount = new BigDecimal("0");
            for (PcComboPro cp: cpList) {
                PcProduct pcProduct = pcProductMapper.selectPcProductByProId(cp.getProId());
                proAllAmount=proAllAmount.add(pcProduct.getRmbPrice().multiply(new BigDecimal(cp.getComboAmount())));
            }
            pro.setTotalProAmount(proAllAmount);
        };
        return pros;
    }

}
